package edu.sctu.yueding.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.utils.ImageLoader;
import edu.sctu.yueding.utils.httputils.GetVolleyHttp;

/**
 * Created by 鑫爷您慢点 on 2016/8/11.
 */
public class ShowPhotoViewPager extends PagerAdapter {

    private List<String> path;
    private boolean isURL;
    private Activity context;
    private ImageView imageView;
    private View itemView;
    private LayoutInflater layoutInflater;


    public ShowPhotoViewPager(List<String> path, boolean isURL, Activity context) {
        this.path = path;
        this.isURL = isURL;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return path.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //if (itemView == null){
            itemView = layoutInflater.inflate(R.layout.item_show_photo_view_pager, null);
            imageView = (ImageView) itemView.findViewById(R.id.img);
        //}

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.finish();
            }
        });
        if (isURL){
            GetVolleyHttp.getInstance((Activity) context).setImageLoader(imageView,
                    R.drawable.photo_pictures_no,
                    R.drawable.photo_pictures_no,
                    path.get(position));
        }else {
            ImageLoader.getInstance(3, ImageLoader.Type.LIFO).loadImageNoCondense(path.get(position), imageView);
        }

        container.addView(itemView);
        return itemView;
    }

}
